package com.yesallchina.tencent.wx.webapp.controller;

import com.yesallchina.wxapp.officialaccount.api.*;
import com.yesallchina.wxapp.officialaccount.api.beans.TextMessage;
import com.yesallchina.wxapp.officialaccount.api.beans.param.PayParam;
import com.yesallchina.wxapp.officialaccount.api.beans.param.QrcodeParam;
import com.yesallchina.wxapp.officialaccount.api.beans.param.ResponseDTO;
import com.yesallchina.wxapp.officialaccount.utils.JacksonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class SampleController {

    @Autowired
    BasicSupportApi basicSupportApi;
    @Autowired
    MessageApi messageApi;
    @Autowired
    UserManageApi userManageApi;
    @Autowired
    AccountManageApi accountManageApi;
    @Autowired
    UIMenuApi menuApi;
    @Autowired
    WebPageBaseSupportApi webPageBaseSupportApi;
    @Autowired
    PayApi payApi;

    @RequestMapping("/hello")
    public String hello() {
        return "Hello world,Welcome to use SpringBoot Framework";
    }

    /**
     * 获取微信服务IP
     *
     * @return
     */
    @RequestMapping("/getWxServerIP")
    public String getWxServerIP() {
        Map<String, Object> result = new HashMap<>();
        result.put("apidomainip", basicSupportApi.getApiDomainIp());
        result.put("callbackip", basicSupportApi.getCallbackIp());
        return JacksonUtils.generate(result);
    }

    /**
     * 获取access_token
     *
     * @return
     */
    @RequestMapping("getAccessToken")
    public String getAccessToken() {
        return JacksonUtils.generate(basicSupportApi.getAccessToken());
    }

    @RequestMapping("jsapiticket")
    public String jsapiticket() {
        return JacksonUtils.generate(webPageBaseSupportApi.getJsapiTicket());
    }


    /**
     * 发送一条客服消息
     *
     * @param openid
     * @return
     */
    @RequestMapping("sendCustomeMessage")
    public String sendCustomeMessage(String openid) {
        TextMessage message = new TextMessage();
        message.setToUserName(openid);
        message.setContent("这是一条客服消息");
        message.setCreateTime(String.valueOf(System.currentTimeMillis()));
        messageApi.sendCustomeMessage(message);
        return "success";
    }

    /**
     * 获取用户基本信息
     *
     * @param openid
     * @return
     */
    @RequestMapping("getUserinfo")
    public String getUserinfo(String openid){
        return JacksonUtils.generate(userManageApi.getUser(openid));
    }


    /**
     * 生成带参数的二维码
     *
     * @return
     */
    @RequestMapping("qrcodeCreate")
    public String qrcodeCreate(){
        QrcodeParam param = new QrcodeParam();
        param.setExpireSeconds(600);
        param.setQrcodeType(QrcodeParam.QrcodeType.TEMPLATE);
        param.setSceneId("123");
        param.setSceneStr("124");
        return JacksonUtils.generate(accountManageApi.qrcodeCreate(param));
    }

    /**
     * 转短连接
     *
     * @return
     */
    @RequestMapping("shorturl")
    public String shorturl(){
        String url = "https://developers.weixin.qq.com/doc/offiaccount/Account_Management/Generating_a_Parametric_QR_Code.html";
        return JacksonUtils.generate(accountManageApi.shorturlCreate(url));
    }

    /**
     * 获取菜单
     *
     * @return
     */
    @RequestMapping("menuGet")
    public String getMenu(){
        return menuApi.queryMenu();
    }

    /**
     * 创建菜单
     *
     * @return
     */
//    @RequestMapping("menuCreate")
//    public String createMenu(){
//        return menuApi.createMenu(menuApi.queryMenu());
//    }

    @RequestMapping("getPayPreSign")
    public String getPayPreSign(){
        PayParam param = new PayParam();
        param.setTradeId("TEST"+String.valueOf(System.currentTimeMillis()));
        param.setBody("会员注册");
        param.setTotalFee(100);
        param.setOpenid("oiwlx1uL31FjFbzQNThqRslDO2P8");
        return ResponseDTO.renderSuccess(payApi.getPrePaySign(param));
    }

}
